/********************************************************************************
 *    Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH    *
 *                                                                              *
 *              This software is distributed under the terms of the             *
 *              GNU Lesser General Public Licence (LGPL) version 3,             *
 *                  copied verbatim in the file "LICENSE"                       *
 ********************************************************************************/
#include "HiMusicCalibratedData.h"
#include "HiMusicWaveEvent.h"

HiMusicCalibratedData::HiMusicCalibratedData() : fEventID(0),
                                                 fDetID(0),
                                                 fNWaves0(0),
                                                 fNWaves1(0),
                                                 fNWaves2(0),
                                                 fNWaves3(0),
                                                 fNWaves4(0),
                                                 fNWaves5(0),
                                                 fNWaves6(0),
                                                 fNWaves7(0),
                                                 fNWaves8(0),
                                                 fTimeStamp(0),
                                                 fTimeStamp_pre(0)
{
    fChanData0 = new TClonesArray("HiMusicWaveEvent");
    fChanData1 = new TClonesArray("HiMusicWaveEvent");
    fChanData2 = new TClonesArray("HiMusicWaveEvent");
    fChanData3 = new TClonesArray("HiMusicWaveEvent");
    fChanData4 = new TClonesArray("HiMusicWaveEvent");
    fChanData5 = new TClonesArray("HiMusicWaveEvent");
    fChanData6 = new TClonesArray("HiMusicWaveEvent");
    fChanData7 = new TClonesArray("HiMusicWaveEvent");
    fChanData8 = new TClonesArray("HiMusicWaveEvent");
}

HiMusicCalibratedData::HiMusicCalibratedData(const HiMusicCalibratedData &data)
    : TObject(data),
      fEventID(data.fEventID),
      fDetID(data.fDetID),
      fNWaves0(data.fNWaves0),
      fNWaves1(data.fNWaves1),
      fNWaves2(data.fNWaves2),
      fNWaves3(data.fNWaves3),
      fNWaves4(data.fNWaves4),
      fNWaves5(data.fNWaves5),
      fNWaves6(data.fNWaves6),
      fNWaves7(data.fNWaves7),
      fNWaves8(data.fNWaves8),
      fTimeStamp(data.fTimeStamp),
      fTimeStamp_pre(data.fTimeStamp_pre)
{
    fChanData0 = new TClonesArray(*data.fChanData0);
    fChanData1 = new TClonesArray(*data.fChanData1);
    fChanData2 = new TClonesArray(*data.fChanData2);
    fChanData3 = new TClonesArray(*data.fChanData3);
    fChanData4 = new TClonesArray(*data.fChanData4);
    fChanData5 = new TClonesArray(*data.fChanData5);
    fChanData6 = new TClonesArray(*data.fChanData6);
    fChanData7 = new TClonesArray(*data.fChanData7);
    fChanData8 = new TClonesArray(*data.fChanData8);
}

HiMusicCalibratedData::~HiMusicCalibratedData()
{
    delete fChanData0;
    delete fChanData1;
    delete fChanData2;
    delete fChanData3;
    delete fChanData4;
    delete fChanData5;
    delete fChanData6;
    delete fChanData7;
    delete fChanData8;
}

void HiMusicCalibratedData::Reset()
{
    fEventID = 0;
    fDetID = 0;
    fNWaves0 = 0;
    fNWaves1 = 0;
    fNWaves2 = 0;
    fNWaves3 = 0;
    fNWaves4 = 0;
    fNWaves5 = 0;
    fNWaves6 = 0;
    fNWaves7 = 0;
    fNWaves8 = 0;
    fTimeStamp = 0;
    fTimeStamp_pre = 0;

    fChanData0->Clear();
    fChanData1->Clear();
    fChanData2->Clear();
    fChanData3->Clear();
    fChanData4->Clear();
    fChanData5->Clear();
    fChanData6->Clear();
    fChanData7->Clear();
    fChanData8->Clear();
}

uint8_t HiMusicCalibratedData::GetNumOfWaves(uint8_t chId)
{
    switch (chId)
    {
    case 0:
        return fNWaves0;
    case 1:
        return fNWaves1;
    case 2:
        return fNWaves2;
    case 3:
        return fNWaves3;
    case 4:
        return fNWaves4;
    case 5:
        return fNWaves5;
    case 6:
        return fNWaves6;
    case 7:
        return fNWaves7;
    case 8:
        return fNWaves8;
    default:
        return 0;
    }
}

TClonesArray *HiMusicCalibratedData::GetChannels(uint8_t chId) const
{
    switch (chId)
    {
    case 0:
        return fChanData0;
    case 1:
        return fChanData1;
    case 2:
        return fChanData2;
    case 3:
        return fChanData3;
    case 4:
        return fChanData4;
    case 5:
        return fChanData5;
    case 6:
        return fChanData6;
    case 7:
        return fChanData7;
    case 8:
        return fChanData8;
    default:
        return nullptr;
    }
}

void HiMusicCalibratedData::SetChannel(uint8_t chId, const TClonesArray *src)
{
    TClonesArray *dest = nullptr;
    Int_t nEntries = src->GetEntries();
    if (chId == 0)
    {
        fNWaves0 = nEntries;
        dest = fChanData0;
    }
    if (chId == 1)
    {
        fNWaves1 = nEntries;
        dest = fChanData1;
    }
    if (chId == 2)
    {
        fNWaves2 = nEntries;
        dest = fChanData2;
    }
    if (chId == 3)
    {
        fNWaves3 = nEntries;
        dest = fChanData3;
    }
    if (chId == 4)
    {
        fNWaves4 = nEntries;
        dest = fChanData4;
    }
    if (chId == 5)
    {
        fNWaves5 = nEntries;
        dest = fChanData5;
    }
    if (chId == 6)
    {
        fNWaves6 = nEntries;
        dest = fChanData6;
    }
    if (chId == 7)
    {
        fNWaves7 = nEntries;
        dest = fChanData7;
    }
    if (chId == 8)
    {
        fNWaves8 = nEntries;
        dest = fChanData8;
    }
    dest->Clear();
    for (Int_t i = 0; i < nEntries; i++)
    {
        HiMusicWaveEvent *data = (HiMusicWaveEvent *)src->At(i);
        new ((*dest)[i]) HiMusicWaveEvent(*data); /// 深拷贝
    }
}
void HiMusicCalibratedData::AddChannel(uint8_t chId, HiMusicWaveEvent &ch)
{
    if (chId == 0)
        new ((*fChanData0)[fNWaves0++]) HiMusicWaveEvent(ch);
    if (chId == 1)
        new ((*fChanData1)[fNWaves1++]) HiMusicWaveEvent(ch);
    if (chId == 2)
        new ((*fChanData2)[fNWaves2++]) HiMusicWaveEvent(ch);
    if (chId == 3)
        new ((*fChanData3)[fNWaves3++]) HiMusicWaveEvent(ch);
    if (chId == 4)
        new ((*fChanData4)[fNWaves4++]) HiMusicWaveEvent(ch);
    if (chId == 5)
        new ((*fChanData5)[fNWaves5++]) HiMusicWaveEvent(ch);
    if (chId == 6)
        new ((*fChanData6)[fNWaves6++]) HiMusicWaveEvent(ch);
    if (chId == 7)
        new ((*fChanData7)[fNWaves7++]) HiMusicWaveEvent(ch);
    if (chId == 8)
        new ((*fChanData8)[fNWaves8++]) HiMusicWaveEvent(ch);
}